package com.application.Adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.application.Activity.R;
import com.application.RowDTO.NeighborRowDTO;

public class NeighborAdapter extends BaseAdapter {
	public static class ViewHolder {
		TextView txtTenRule;
		CheckBox chkChon;
	}

	private Activity activity;
	private LayoutInflater inflater;
	private ArrayList<NeighborRowDTO> data;

	public NeighborAdapter(Activity activity, ArrayList<NeighborRowDTO> data) {
		this.activity = activity;
		this.data = data;
		this.inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public Object getItem(int position) {

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			view = inflater.inflate(R.layout.listrow_rule, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.chkChon = (CheckBox) view.findViewById(R.id.chkChon);
			view.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) view.getTag();		
		holder.chkChon.setText(data.get(position).neighbor.get_member().get_fullName());
		final int index = position;		
		holder.chkChon
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						data.get(index).checked = isChecked;
					}
				});
		holder.chkChon.setChecked(data.get(index).checked);		
		return view;
	}

}
